package controller

import (
	"fx_swift_ifcgate_agent/api"
	"fx_swift_ifcgate_agent/internal/service"
	"fx_swift_ifcgate_agent/pkg/code"
	"fx_swift_ifcgate_agent/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 新增邀请码
func CreateInvitationCode(c *gin.Context) {
	req := &api.InvitationCodeReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.(internalerrors.ServiceError).Msg)
		return
	}

	result, err := service.NewInvitationSrv().CreateInvitationCode(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 查看邀请成功统计详情
func ViewInvitationStatistics(c *gin.Context) {
	invitId := c.Query("invitId")
	result, err := service.NewInvitationSrv().ViewInvitationStatistics(c, invitId)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 邀请码列表
func QueryInvitationList(c *gin.Context) {
	req := &api.InvitationListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.(internalerrors.ServiceError).Msg)
		return
	}

	result, err := service.NewInvitationSrv().QueryInvitationList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
